package com.sf.osms.dep.processor.util;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.sf.osms.dep.cache.SysConfigCacheHelper;

public class FTPUtil {
	
	public static final String FTP_URL = "202.66.140.166";
	public static final int FTP_PORT = 21;
	public static final String FTP_USERNAME = "jpAdmin";
	public static final String FTP_PASSWORD = "Seibu!0514";
	public static final String FTP_DOWNLOAD_DIRECTORY = "/nfsc/ASURA_OSMS_SERVER/DEP_DOWNLOAD_DIRECTORY";
	/**
	 * 进行远程路径下面文件的下载
	 * @param remotePath 远程文件下载路径
	 * @param remoteBackupPath 远程文件备份路径
	 * @param filePrefix 需要下载文件的前缀，若没有限制传null
	 * @param fileSuffix 需要下载文件的后缀，若没有限制传null
	 * @param agentCode 
	 * @param agentInfo 
	 * @return 本地文件路径的集合
	 * @throws Exception
	 */
	public static List<String> downloadTrackFiles(String remotePath,String remoteBackupPath,String filePrefix,String fileSuffix, String agentCode) throws Exception{
		if(StringUtils.isEmpty(remotePath) || StringUtils.isEmpty(remoteBackupPath)){
			return null;
		}
	/*	Properties prop = AppContext.getContext().getConfigProperties();
		String ftpUrl = (String) prop.get("ftp_url");*/
		String ftpUrl = SysConfigCacheHelper.getSysConfigValue("ftp_url");
		if(StringUtils.isBlank(ftpUrl)){
			ftpUrl = FTP_URL;
		}
		ftpUrl = ftpUrl.trim();
		/*String ftpUsername = (String) prop.get("ftp_username");*/
		String ftpUsername = SysConfigCacheHelper.getSysConfigValue("ftp_username");
		if(StringUtils.isBlank(ftpUsername)){
			ftpUsername = FTP_USERNAME;
		}
		ftpUsername = ftpUsername.trim();
		
		/*String ftpPassword = (String) prop.get("ftp_password");*/
		String ftpPassword = SysConfigCacheHelper.getSysConfigValue("ftp_password");
		if(StringUtils.isBlank(ftpPassword)){
			ftpPassword = FTP_PASSWORD;
		}
		ftpPassword = ftpPassword.trim();
		
		Integer ftpPort = FTP_PORT;
		try {
			/*String ftpPortStr = (String) prop.get("ftp_port");*/
			String ftpPortStr = SysConfigCacheHelper.getSysConfigValue("ftp_port");
			ftpPort =  Integer.valueOf(ftpPortStr.trim());
		} catch (Exception e) {
			ftpPort = FTP_PORT;
		}
		
		/*String downloadPrefix = (String) prop.get("dep.ftp.download.dir");*/
		String downloadPrefix = SysConfigCacheHelper.getSysConfigValue("dep.ftp.download.dir");
		if(StringUtils.isBlank(downloadPrefix)){
			downloadPrefix = FTP_DOWNLOAD_DIRECTORY;
		}
		String downloadPrefixDir = downloadPrefix + "/" + agentCode; //每个代理对应一个文件处理目录
		//进行代理文件的清理
		//FileUtil.deleteDirectory(downloadPrefixDir);
		FileUtil.createDirIfNot(downloadPrefixDir);
		
		//删除下载临时目录中的文件
		
		FTPConnectInfo connectInfo = new FTPConnectInfo(ftpUrl, ftpPort, ftpUsername, ftpPassword,filePrefix,fileSuffix);
		FTPTool ft = new FTPTool(connectInfo);
		//本地文件路径由本地路径前缀+远程路径(远程路径一定为/开头)
		if(!remotePath.startsWith("/")){
			remotePath = "/" + remotePath;
		}
		
		List<String> list = ft.downloadFiles(remotePath, downloadPrefixDir,remoteBackupPath);
		return list;
	}
	
	
	public static void main(String[] args) throws Exception{
		/*List<String> list = downloadTrackFiles("/test","/test_build/backup",null,"csv,txt");
		for (String string : list) {
			System.out.println(string);
		}*/
		List<String> list = null;
		for (String string : list) {
			System.out.println(string);
		}
	}
	
}
